package top.fzqblog.firstapp;
import android.animation.Animator;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.BlurTransformation;
import top.fzqblog.util.StatusBarUtil;

/**
 * Created by 抽离 on 2017/9/20 0020.
 */

public class CollapsingToolbarLayoutTest extends AppCompatActivity {


    @BindView(R.id.toolbar_layout)
    public CollapsingToolbarLayout collapsingToolbarLayout;

    @BindView(R.id.collapsing_tool_bar)
    public Toolbar toolbar;

    @BindView(R.id.imageView)
    public ImageView imageView;

    @BindView(R.id.appbarLayout)
    public AppBarLayout appBarLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.collspsing_toolbar_test);
        ButterKnife.bind(this);
        imageView.setVisibility(View.INVISIBLE);
        Glide
                .with(this)
                .load(R.mipmap.mohu)
                .skipMemoryCache( true )
                .bitmapTransform(new BlurTransformation(this, 24, 4))
                .listener(new RequestListener<Integer, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, Integer model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }
                    @Override
                    public boolean onResourceReady(GlideDrawable resource, Integer model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        int finalRadius = Math.max(imageView.getWidth(), imageView.getHeight());
                        Animator anim = ViewAnimationUtils.createCircularReveal(imageView, 10, imageView.getHeight(), 0, finalRadius);
                        imageView.setVisibility(View.VISIBLE);
                        anim.setDuration(400);
                        anim.start();
                        return false;
                    }
                })
                .into(imageView);

//        imageView.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        }, 500);

        initTranslucentBar();
        toolbar.setTitle("App Title"); //设置Toolbar标题

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void initTranslucentBar() {
        StatusBarUtil.setTranslucentForImageView(  this, 0, collapsingToolbarLayout);
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) collapsingToolbarLayout.getLayoutParams();
        layoutParams.setMargins(0, -StatusBarUtil.getStatusBarHeight(this), 0, 0);//
        ViewGroup.MarginLayoutParams layoutParams2 = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams();
        layoutParams2.setMargins(0, StatusBarUtil.getStatusBarHeight(this), 0, 0);//设置toolbar距离上一层有一个状态栏的高度
    }

    /**
     * 返回按钮
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
